package com.liushuai.demotestcxf.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.liushuai.demotestcxf.api.InitBillReq;
import com.liushuai.demotestcxf.api.InitBillRsp;
import com.liushuai.demotestcxf.entity.User;
import com.liushuai.demotestcxf.service.RedisService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

/**
 * Created by on 2017/3/1.
 */
@Controller
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    RedisService redisService;

    /**
     * 设置Str缓存
     *
     * @param key
     * @param val
     * @return
     */
    @RequestMapping(value = "setStr")
    public String setStr(String key, String val) {
        try {
            redisService.setStr(key, val);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 根据key查询Str缓存
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "getStr")
    public String getStr(String key) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8099/test/user?wsdl");
        //getUser 为接口中定义的方法名称  张三为传递的参数   返回一个Object数组
        Object[] objects = new Object[0];
        XmlMapper mapper =new XmlMapper();
        InitBillReq req=new InitBillReq();
        String xmlStr = null;
        try {
            xmlStr = mapper.writeValueAsString(req);
            System.out.println(xmlStr);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        try {
            objects = client.invoke("getUser", xmlStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        InitBillRsp rsp = null;
        try {
            rsp = mapper.readValue(objects[0].toString(), InitBillRsp.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //输出调用结果
        System.out.println("*****" + objects[0].toString());
        return redisService.getStr(key);
    }

    /**
     * 根据key产出Str缓存
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "delStr")
    public String delStr(String key) {
        try {
            redisService.del(key);
            return "success";
        } catch (Exception e) {
            return "error";
        }
    }


    /**
     * 设置obj缓存
     *
     * @param key
     * @param user
     * @return
     */
    @RequestMapping(value = "setObj")
    public String setObj(String key, User user) {
        try {
            redisService.setObj(key, user);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 获取obj缓存
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "getObj")
    public Object getObj(String key) {
        return redisService.getObj(key);
    }


    /**
     * 删除obj缓存
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "delObj")
    public Object delObj(String key) {
        try {
            redisService.delObj(key);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

}
